layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var layer = layui.layer;
    /**
     * 服务详情管理
     */
    var DbServicedetails = {
        tableId: "dbServicedetailsTable"
    };

    /**
     * 初始化表格的列
     */
    DbServicedetails.initColumn = function () {
        return [[
            {type: 'numbers', title: '序号', width: 100},
            {field: 'id', hide: true, title: 'id'},
            {field: 'serviceId', sort: false, title: '服务id', hide: true},
            {field: 'tableName', sort: false, title: '表名称', width: 200, align: "center"},
            {field: 'type', sort: false, title: '类型', hide: true},
            {field: 'details', sort: false, title: '说明'},
            {field: 'revision', sort: false, title: '乐观锁', hide: true},
            {field: 'createdBy', sort: false, title: '创建人', hide: true},
            {field: 'createdTime', sort: false, title: '创建时间', hide: true},
            {field: 'updatedBy', sort: false, title: '更新人', hide: true},
            {field: 'updatedTime', sort: false, title: '更新时间', hide: true},
            {align: 'center', toolbar: '#tableBar', title: '操作', hide: true}
        ]];
    };

    /**
     * 获取表单详情，获取详情信息，填充表单
     */

    $("#btnAddTabs").ready(function () {
        if (Feng.getUrlParam("btnStatus") == 1) {
            $("#btnAddTabs").remove();
        }
    })

    var ajax = new $ax(Feng.ctxPath + "/databaseListKh/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    $("#id").val(result.data.id);
    $("#standard").html(result.data.standard);
    $("#system").html(result.data.system);
    $("#details").html(result.data.details);
    if (result.data.servicetype == '1') {
        $("#servicetype").text('数据库服务')
    }
    // console.log(result)


    //监听行单击事件（双击事件为：rowDouble）
    table.on('row(dbServicedetailsTable)', function (obj) {
        var data = obj.data;
        window.location.href = Feng.ctxPath + '/dbServicedetails/tableDetails?serviceId=' + data.serviceId + '&tableName=' + data.tableName
    });

    //点击返回按钮
    $("#returnBefore").click(function () {
        history.go(-1)
    })

    /**
     * 修改密码
     */
    $("#changePassword").click(function () {
        layer.open({
            type: 2,
            title: '',
            area: ['450px', '344px'],
            content: Feng.ctxPath + '/databaseListKh/alter_password?id=' + Feng.getUrlParam("id"),
        })
    })


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DbServicedetails.tableId,
        url: Feng.ctxPath + '/dbServicedetails/list?serviceId=' + Feng.getUrlParam("id"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: "line",
        cols: DbServicedetails.initColumn(),
        done: function () {
            $('tr').css('background-color', '#ffffff');
            $('td').css('cursor', 'pointer');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        DbServicedetails.search();
    });

    // 添加按钮点击事件
    $('#btnAddTabs').click(function () {
        DbServicedetails.openAddTabDlg();
    });


    // 工具条点击事件
    table.on('tool(' + DbServicedetails.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DbServicedetails.openEditDlg(data);
        } else if (layEvent === 'delete') {
            DbServicedetails.onDeleteItem(data);
        }
    });
    // 申请升级
    $('#btnApp').click(function () {
        //获取选中数据
        var checkStatus = table.checkStatus('dbServicelistTable'),
            data = checkStatus.data;
        if (data.length != 1) {
            Feng.error("请选择一个哦")
            return
        } else {
            admin.open({
                type: 2,
                title: '',
                area: ['700px', '480px'],
                content: Feng.ctxPath + '/dbServicedetails/add?serviceId=' + Feng.getUrlParam("id"),
                tableId: DbServicedetails.tableId,
                end: function () {
                    table.reload(DbServicedetails.tableId);
                }
            });
        }
    });
    // 启动
    $('#btnStar').click(function () {

        var btnName = 'btnStar';
        DbServicedetails.btnsEdit(btnName);

    });
    // 停止
    $('#btnStop').click(function () {

        var btnName = 'btnStop';
        DbServicedetails.btnsEdit(btnName);
    });
    // 修改密码
    $('#btnEditPassword').click(function () {
        var btnName = 'btnEditPassword';
        DbServicedetails.btnsEdit(btnName);
    });
    // 废弃
    $('#btnKil').click(function () {

        var btnName = 'btnKil';
        DbServicedetails.btnsEdit(btnName);
    });
    /**
     * 修改启动升级等操作
     */
    DbServicedetails.btnsEdit = function (btnName) {

        var ajax = new $ax(Feng.ctxPath + "/databaseListKh/detail?id=" + Feng.getUrlParam("id"));
        var result = ajax.start();
        if (btnName == 'btnEditPassword') {
            layer.prompt({title: '输入新的密码', formType: 1}, function (pass, index) {
                layer.close(index);
                var password = pass + '_password'
                result.data.password = password;
                var ajax = new $ax(Feng.ctxPath + "/databaseListKh/editItem", function (data) {
                    if (data.code == 200) {
                        Feng.success("修改成功！");
                    } else if (data.code == 500) {
                        Feng.error(data.message);
                    }
                }, function (data) {
                    Feng.error("更新失败！" + data.responseJSON.message)
                });
                ajax.set(result.data);
                ajax.start();
            });
        } else {
            if (btnName == 'btnStar') {
                result.data.state = 1;
            } else if (btnName == 'btnStop') {
                result.data.state = 0;
            } else if (btnName == 'btnKil') {
                result.data.state = '2_kil';
            }
            var ajax = new $ax(Feng.ctxPath + "/databaseListKh/editItem", function (data) {
                Feng.success("更新成功！");
                table.reload(DbServicedetails.tableId);
            }, function (data) {
                Feng.error("更新失败！" + data.responseJSON.message)
            });
            ajax.set(result.data);
            ajax.start();
        }


    };
});
